// pages/post-photo/post-photo.js
import { Photo } from './photo-model.js';
import { Config } from '../../utils/config.js';
var photo = new Photo();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    petsList: [],
    index: 0,
    upload_img_list: [],
    uploadType: [
      { name: 'image', value: '上传照片', checked: 'true' },
      { name: 'video', value: '上传视频' },
    ],
    chooseType: 'image',
    video: ''
  },

  bindPickerChange: function (e) {
    this.setData({
      pets_id: this.data.petsIndex[e.detail.value]
    })
  },

  _loadData: function (e) {
    var that = this;

    photo.getMyPets((res) => {
      console.log(res)
      that.setData({
        petsList: res,
      })
    })

  },

  checkboxChange: function (e) {
    console.log(e.default.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },

  // 上传图片
  uploadPic: function () {
    var self = this;
    var resArr = [];

    wx.chooseImage({
      count: 9,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;

        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: Config.restUrl + 'file/upload',
            filePath: tempFilePaths[i],
            name: 'file',
            success: function (res) {
              var res = JSON.parse(res.data);
              if (res.code) {
                resArr.push(res.file);
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 1000
                })

                self.setData({
                  upload_img_list: self.data.upload_img_list.concat(res.file)
                })

              } else {
                wx.showToast({
                  title: res.error,
                  icon: 'none',
                  duration: 1000
                })
              }
            },
            fail: function ({ errMsg }) {
              wx.showToast({
                title: errMsg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        }

      },
      fail: function ({ errMsg }) {
        console.log('chooseImage fail, err is', errMsg)
      }
    })
  },

  uploadVideo: function () {
    var that = this;

    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        var size = (res.size / (1024 * 1024)).toFixed(2);
        if (size > 3) {
          wx.showModal({
            title: '微宠相册',
            content: '很抱歉，视频最大允许3M，当前为' + size + 'M'
          })
          return false;
        }
        wx.showLoading({ title: '视频上传中' });
        that.setData({
          video: res.tempFilePath,
          size: size
        })
        wx.uploadFile({
          url: Config.restUrl + 'file/upload',
          method: 'POST',
          filePath: that.data.video,
          name: 'file',
          success: function (res) {
            var res = JSON.parse(res.data);
            if (res.code) {
              wx.hideLoading();
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1000
              })
              console.log(res.file)
              that.setData({
                video: res.file
              })

            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 1000
              })
            }
          },
          fail: function ({ errMsg }) {
            wx.showToast({
              title: errMsg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },

  radioChange: function (e) {
    var that = this;
    that.setData({
      chooseType: e.detail.value
    })
    if (e.detail.value == 'image') {
      that.setData({
        video: ''
      })
    } else if (e.detail.value == 'video') {
      that.setData({
        upload_img_list: []
      })
    }
  },

  formSubmit: function (e) {
    if (!e.detail.value.content) {
      wx.showToast({
        title: '请输入相册简介',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (this.data.chooseType == 'image') {
      if (e.detail.value.img.length < 1) {
        wx.showToast({
          title: '请上传宠物照片',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    } else {
      if (!e.detail.value.video.length) {
        wx.showToast({
          title: '请上传宠物视频',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    }

    if (e.detail.value.content.length > 100) {
      wx.showToast({
        title: '相册简介最多输入100个字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (e.detail.value.pets_id.length < 1) {
      wx.showToast({
        title: '请选择宠物',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    photo.createPhoto(e.detail.value, (data) => {
      if (data.code) {
        wx.showToast({
          title: data.info,
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 2000)

      } else {
        wx.showToast({
          title: res.error,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

})